﻿
CREATE PROCEDURE [dbo].[proc_Project_Create]
(
	@OrderId uniqueidentifier,
	@ProductId int,
	@ProductName nvarchar(50),
	@SupplierId int,
	@SupplierName nvarchar(50),
	@CostPrice decimal(18,2),
	@SalePrice decimal(18,2),
	@Description nvarchar(200),
	@TypeId int,
	@TypeName nvarchar(50),
	@IsFinish bit,
	@CreateDate datetime,
	@CreateUser nvarchar(50)
)
AS
Begin
	--订单数据
	Declare @CompanyId int,@CusId uniqueidentifier,@CusName nvarchar(60)
	Declare @Salesman nvarchar(20),@SalesmanAccount varchar(20)
	Declare @NOE_Flag int,@StateId int,@DeptId int

	Select
	@CompanyId=CompanyId,
	@DeptId=DepId,
	@CusId=CusId,
	@CusName=CusName,
	@Salesman=Salesman,
	@SalesmanAccount=SalesmanAccount,
	@StateId=StateId,
	@NOE_Flag=NOE_Flag
	From [Order]
	Where OId=@OrderId

	If @StateId<>2
		Return
	
	--项目执行数据
	Declare @ExecPerson int , @ExecDepartmentId int
	Select @ExecPerson=ISNULL(UserName,'0'),@ExecDepartmentId=ISNULL(DepartmentId,'0') From ProjectInterfaceSet Where ProductId=@ProductId And CompanyId=@CompanyId

	Declare @SCateOneId int, @SCateTwoId int, @SubjectId int
	Select @SCateOneId=SCateOneId,@SCateTwoId=SCateTwoId,@SubjectId=SubjectId From [Product] Where Id=@ProductId
	
	Insert Into Project
		(
			OrderId,
			CusId,
			CusName,
			ProductId,
			ProductName,
			SupplierId,
			SupplierName,
			CostPrice,
			SalePrice,
			Description,
			TypeId,
			TypeName,
			IsFinish,
			CompanyId,
			DeptId,
			Salesman,
			SalesmanAccount,
--			CreateDate,
			CreateUser,
			ExecPerson,
			ExecDepartmentId,
			FinanceAudit,
			SCateOneId,
			SCateTwoId,
			SubjectId,
			NOE_Flag
		)
	Values
		(
			@OrderId,
			@CusId,
			@CusName,
			@ProductId,
			@ProductName,
			@SupplierId,
			@SupplierName,
			@CostPrice,
			@SalePrice,
			@Description,
			@TypeId,
			@TypeName,
			@IsFinish,
			@CompanyId,
			@DeptId,
			@Salesman,
			@SalesmanAccount,
--			@CreateDate,
			@CreateUser,
			@ExecPerson,
			@ExecDepartmentId,
			1,
			@SCateOneId,
			@SCateTwoId,
			@SubjectId,
			@NOE_Flag
		)
End